package com.edu.exam.entity.quality.workflow;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 审核规则实体类
 * 对应数据库表：audit_rule
 *
 * 根据B-02-05试题审核流程子模块设计文档实现
 * 管理基于Drools规则引擎的审核规则配置，支持质量、合规、业务三种规则类型。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Table(name = "audit_rule")
@Comment("审核规则表")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AuditRule {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 审核规则唯一标识符
     */
    @NotBlank(message = "审核规则ID不能为空")
    @Size(max = 50, message = "审核规则ID长度不能超过50个字符")
    @Column(name = "rule_id", nullable = false, length = 50, unique = true)
    private String ruleId;

    /**
     * 审核规则代码
     */
    @NotBlank(message = "审核规则代码不能为空")
    @Size(max = 32, message = "审核规则代码长度不能超过32个字符")
    @Column(name = "rule_code", nullable = false, length = 32, unique = true)
    private String ruleCode;

    /**
     * 审核规则名称
     */
    @NotBlank(message = "审核规则名称不能为空")
    @Size(max = 200, message = "审核规则名称长度不能超过200个字符")
    @Column(name = "rule_name", nullable = false, length = 200)
    private String ruleName;

    /**
     * 审核规则描述
     */
    @Column(name = "rule_description", columnDefinition = "TEXT")
    private String ruleDescription;

    /**
     * 审核规则类型（1-质量规则，2-合规规则，3-业务规则）
     */
    @NotNull(message = "审核规则类型不能为空")
    @Column(name = "rule_type", nullable = false)
    private Integer ruleType;

    /**
     * 审核规则状态（ACTIVE-激活，INACTIVE-非激活，DEPRECATED-已废弃）
     */
    @NotBlank(message = "审核规则状态不能为空")
    @Size(max = 20, message = "审核规则状态长度不能超过20个字符")
    @Column(name = "rule_status", nullable = false, length = 20)
    private String ruleStatus;

    /**
     * 规则优先级（1-最高，依次递减）
     */
    @NotNull(message = "规则优先级不能为空")
    @Column(name = "priority", nullable = false)
    private Integer priority;

    /**
     * 是否激活
     */
    @NotNull(message = "激活状态不能为空")
    @Column(name = "is_active", nullable = false)
    private Boolean isActive;

    /**
     * Drools规则表达式
     */
    @NotBlank(message = "Drools规则表达式不能为空")
    @Column(name = "drools_expression", nullable = false, columnDefinition = "TEXT")
    private String droolsExpression;

    /**
     * 规则条件（JSON格式）
     */
    @Column(name = "rule_conditions", columnDefinition = "TEXT")
    private String ruleConditions;

    /**
     * 规则动作（JSON格式）
     */
    @Column(name = "rule_actions", columnDefinition = "TEXT")
    private String ruleActions;

    /**
     * 适用题型（JSON格式）
     */
    @Column(name = "applicable_question_types", columnDefinition = "TEXT")
    private String applicableQuestionTypes;

    /**
     * 规则参数（JSON格式）
     */
    @Column(name = "rule_parameters", columnDefinition = "TEXT")
    private String ruleParameters;

    /**
     * 规则模板
     */
    @Size(max = 50, message = "规则模板长度不能超过50个字符")
    @Column(name = "rule_template", length = 50)
    private String ruleTemplate;

    /**
     * 规则版本
     */
    @Size(max = 20, message = "规则版本长度不能超过20个字符")
    @Column(name = "rule_version", length = 20)
    private String ruleVersion;

    /**
     * 创建者ID
     */
    @Size(max = 50, message = "创建者ID长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 软删除标识
     */
    @NotNull(message = "软删除标识不能为空")
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", nullable = false, length = 50)
    private String updatedBy;

}